﻿////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// <copyright>Copyright 2008-2011 Andreas Huber Doenni</copyright>
// Distributed under the GNU General Public License version 2 (GPLv2).
// See accompanying file License.txt or copy at http://phuse.codeplex.com/license.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

namespace Microsoft.Phone.Data.Linq
{
    using System.Data.SqlServerCe;
    using System.Linq;

    using Phuse.Net.Forums;

    /// <summary>This duplicates the functionality of a class with the same name on Windows Phone 7.5.</summary>
    internal sealed class DatabaseSchemaUpdater
    {
        private readonly int databaseSchemaVersion;

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////

        internal DatabaseSchemaUpdater(Database database)
        {
            try
            {
                this.databaseSchemaVersion = (from version in database.VersionTable select version.SchemaVersion).Max();
            }
            catch (SqlCeException ex)
            {
                // These values mean that the table is missing. If so, the default value of 0 for the version is fine.
                if ((ex.NativeError != 0) || (ex.HResult != -2147217865))
                {
                    throw;
                }
            }
        }

        internal int DatabaseSchemaVersion
        {
            get { return this.databaseSchemaVersion; }
        }
    }
}
